package com.dahuyou.method.record;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.AdviceAdapter;

public class MethodRecordingVisitorAdvice extends AdviceAdapter {
    private final String className;
    private String methodName;

    protected MethodRecordingVisitorAdvice(int access, String methodName, String desc, MethodVisitor mv, String className, String fullClassName, String simpleClassName) {
        super(ASM5, mv, access, methodName, desc);
        this.methodName = methodName;
        this.className = className;
    }

    @Override
    protected void onMethodEnter() {
        // public static void printLog(String className, String methodName) {
        mv.visitLdcInsn(className);
        mv.visitLdcInsn(methodName);
        mv.visitMethodInsn(INVOKESTATIC, "com/dahuyou/method/record/LogUtil", "printLog", "(Ljava/lang/String;Ljava/lang/String;)V", false);
    }

    /**
     * 方法执行结束前调用，即return，throw异常前调用
     *
     * @param opcode 操作码 org.objectweb.asm.Opcodes 比如方法的public private啊，jdk的版本号啊，位运算啊，各种字节码指令啊当然包含这个方法会传入的如下可能的操作码：
     * int IRETURN = 172; // 返回int
     * int LRETURN = 173; // 返回long
     * int FRETURN = 174; // 返回float
     * int DRETURN = 175; // 返回double
     * int ARETURN = 176; // 返回引用类型
     * int RETURN = 177; // 返回void
     * 这里就可以通过opcode知道方法返回的值是什么了！
     */
    @Override
    protected void onMethodExit(int opcode) {
        super.onMethodExit(opcode);
    }
}
